<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class AuthMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
//        $path = $request->path();
//        if ($path == 'login') {
//            if (session()->has('user')) {
//                return redirect('/home');
//            }
//        } else {
//            if (!session()->has('user')) {
//                return redirect('/login');
//            }
//        }
        $token = $request->header('token');
        if (!$token) {
            return response()->json([], 403);
        }
//        dd($token);
        $user = \App\Models\User::where('remember_token', $token)->first();
        if (!$user) {
            return response()->json([], 403);
        }
//        $token =
        return $next($request);
    }
}
